package com.lxb.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.lxb.commonutils.R;
import com.lxb.servicebase.exceptionhandler.GuliException;
import com.lxb.vod.service.VodService;
import com.lxb.vod.util.ConstantPropertiesUtils;
import com.lxb.vod.util.InitAcsClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author xuebin.li
 * @createTime 2022/11/29 21:41
 */
@Service
public class VodServiceImpl implements VodService {


    /**
     * 文件流上传视频
     *
     * @param file
     * @return
     */
    @Override
    public String upLoadVideoAliYun(MultipartFile file) {

        try {
            String videoId = null;

            //title 上传到阿里云的视频名称
            String[] titleList = file.getOriginalFilename().split("\\.");
            String title = titleList[0];
            //fileName  上传文件的原始名称
            String fileName = file.getOriginalFilename();
            //inputStream 上传文件输入流
            InputStream inputStream = file.getInputStream();

            UploadStreamRequest request = new UploadStreamRequest(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
            if (response.isSuccess()) {
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                //获取视频ID
                videoId = response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                //            System.out.print("ErrorCode=" + response.getCode() + "\n");
                //            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
                //获取视频ID
                videoId = response.getVideoId();
            }
            return videoId;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 删除多个视频
     *
     * @param vodIds
     */
    @Override
    public void deleteBath(List<String> vodIds) {
        try {
            //初始化 client 对象
            DefaultAcsClient client = InitAcsClient.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            //创建删除视频 request 和 response 对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            DeleteVideoResponse response = new DeleteVideoResponse();

            //创建请求对象
            //一次只能请求删除20个视频
            String join = StringUtils.join(vodIds, ",");
            //向request对象设置视频id
            request.setVideoIds(join);
            //client 执行删除
            response = client.getAcsResponse(request);

            System.out.print("RequestId = " + response.getRequestId() + "\n");
        } catch (ClientException e) {
            throw new GuliException(20001, "删除视频失败");
        }
    }
}
